package com.camerareader.mvp;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.Html;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.camerareader.R;
import com.camerareader.common.LoadingBitmapListener;
import com.camerareader.mvp.main_page.MainActivity;
import com.nettools.Constants;
import com.nettools.NetTools;
import com.nettools.NetUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Response;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class OrderInfoActivity extends AppCompatActivity {

    TextView orderPrompt;
    TextView orderNo,orderTime,payType;
    TextView productName,oldPrice,nowPrice,buySize;
    TextView Modelling;//化妆/造型说明
    TextView Service_Time;//服务时间
    TextView Shot_Number;//拍摄照片数量
    TextView Refinement_Number;//精修照片数量
    EditText orderZkEt,orderPriceEt;
    ImageView imageView;
    private String prderId;
    private double nowPriceValue = 0.0f;
    OrderInfoModel model;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_info);

        initViews();
    }

    private void initViews() {
        prderId = getIntent().getStringExtra("orderId");
        model = (OrderInfoModel) getIntent().getSerializableExtra("orderModel");

        imageView = findViewById(R.id.imageView);
        orderNo = findViewById(R.id.orderNo);
        orderTime = findViewById(R.id.orderTime);
        payType = findViewById(R.id.payType);

        Modelling = findViewById(R.id.Modelling);
        Service_Time = findViewById(R.id.Service_Time);
        Shot_Number = findViewById(R.id.Shot_Number);
        Refinement_Number = findViewById(R.id.Refinement_Number);

        productName = findViewById(R.id.productName);
        oldPrice = findViewById(R.id.oldPrice);
        nowPrice = findViewById(R.id.nowPrice);
        buySize = findViewById(R.id.buySize);

        orderNo.setText("订单编号："+model.Order_Num);
        orderTime.setText("下单时间："+model.Create_Time);
        productName.setText("商品名称："+model.Product_Title);
        oldPrice.setText("原价：￥"+model.Actual_Total);
        nowPrice.setText("商品价格：￥"+model.Actual_Total);
        buySize.setText("x"+model.Buy_Number);

        Modelling.setText(model.Modelling);
        Service_Time.setText(model.Service_Time);
        Shot_Number.setText(model.Shot_Number+"张");
        Refinement_Number.setText(model.Refinement_Number+"张");

        nowPriceValue = model.Actual_Total;


        orderZkEt =findViewById(R.id.orderZkEt);
        orderZkEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String st = s.toString();
                if(st.length() < 1){
                    st = "1";
                }
                float v = Float.parseFloat(st);
                orderPriceEt.setText(""+(v*nowPriceValue));
            }
        });
        orderPriceEt = findViewById(R.id.orderPriceEt);
        orderPriceEt.setEnabled(false);
        orderPriceEt.setText(nowPriceValue+"");

        orderPrompt = findViewById(R.id.order_prompt);
        orderPrompt.setText(Html.fromHtml(model.Product_Describe));
//        orderPrompt.setText(Html.fromHtml("<font color='#94c65c'>· 前台接待</font><br/>前台客服会询问你的证件照用途，帮你选择合适的底色和尺寸。<br/><font color='#94c65c'>· 化妆造型</font><br/>专业化妆师会根据你的气质和职业属性量身定做妆容和发型，让形容得到提升。<br/><font color='#94c65c'>· 服饰搭配</font><br/>可提供部分正装，建议你自带服装和配饰(更合身效果更好)，店内也有部分配饰可供选择。"));

        findViewById(R.id.submit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onSubmit();
            }

        });

        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OrderInfoActivity.this.finish();
            }
        });
        loadImage();
    }

    private void loadImage() {
        String url = Constants.HOST_URL + model.Product_Cover;
        Log.w("url....", url);
        Glide.with(this)
                .load(url)
                .placeholder(ContextCompat.getDrawable(this, R.drawable.image_placeholder))
                .listener(new LoadingBitmapListener<String, GlideDrawable>())
                .into(imageView);
    }

    private void onSubmit() {

        Observable.create(new Observable.OnSubscribe<JSONObject>() {
            @Override
            public void call(Subscriber<? super JSONObject> subscriber) {
                try {
                    Response response = NetUtils.getInstance().sendOrderPrice(prderId,orderPriceEt.getText().toString());
                    JSONObject json = new JSONObject(response.body().string());
                    System.out.println(json.toString());
                    if (json.optBoolean(Constants.STATUS)){
                        subscriber.onNext(json);
                    }else {
                        subscriber.onError(new Exception(json.optString(Constants.MSG)));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                subscriber.unsubscribe();
            }
        }).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<JSONObject>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(OrderInfoActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onNext(JSONObject jsonObject) {
                        Toast.makeText(OrderInfoActivity.this, "发送价格成功", Toast.LENGTH_SHORT).show();
                        OrderInfoActivity.this.finish();
                    }
                });
    }
}
